package com.clannad.eduservice.controller;


import com.clannad.commonutils.R;
import com.clannad.eduservice.entity.EduVideo;
import com.clannad.eduservice.feign.VodClient;
import com.clannad.eduservice.service.EduVideoService;
import com.clannad.servicebase.exceptionhandler.GuliException;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 课程视频 前端控制器
 * </p>
 *
 * @author clannad
 * @since 2022-08-11
 */
@RestController
@RequestMapping("/eduservice/video")
@Api(tags = {"小节管理"})
//@CrossOrigin
public class EduVideoController {
    @Autowired
    private EduVideoService eduVideoService;

    @Autowired
    private VodClient vodClient;

    @PostMapping("addOrUpdateVideo")
    public R addOrUpdateVideo(@RequestBody EduVideo video){
        eduVideoService.saveOrUpdate(video);
        return R.ok();
    }

    @DeleteMapping("{id}")
    public R deleteVideo(@PathVariable String id){
        EduVideo byId = eduVideoService.getById(id);
        if(!StringUtils.isEmpty(byId.getVideoSourceId())){
            vodClient.removeAliyunVideo(byId.getVideoSourceId());
        }
        boolean b = eduVideoService.removeById(id);
        if(!b){
            throw new GuliException(20001,"删除失败");
        }
        return R.ok();
    }

    @GetMapping("getVideo/{id}")
    public R getVideo(@PathVariable String id){
        EduVideo byId = eduVideoService.getById(id);
        return R.ok().data("video",byId);
    }

}

